package ru.ifmo.croak.impl.stress.query.team1;

import ru.ifmo.croak.api.Message;
import ru.ifmo.croak.api.MessageListener;

import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Roman Kolganov
 * 27.05.11
 */
public class CountingMessageListener implements MessageListener {

    private final AtomicLong received;
    private final QueryRequestGenerator generator;
    private final Random rand = new Random();

    public CountingMessageListener(AtomicLong received, QueryRequestGenerator generator) {
        this.received = received;
        this.generator = generator;
    }

    public void messageAvailable(Message message) {
            received.addAndGet(message.getText().length());
            generator.addAuthor(message.getAuthor(), rand);
            StringTokenizer st = new StringTokenizer(message.getText());
            while (st.hasMoreTokens()) {
                generator.addWord(st.nextToken(), rand);
            }
        }
}
